#ifndef FORMALBUMMIXER_H
#define FORMALBUMMIXER_H

#include <QtGui/QWidget>
#include <QModelIndex>
#include <QPixmap>
#include <QThread>
#include <QListWidget>
#include <QMap>
#include <QMutex>
#include <QSemaphore>

#include "profile.h"

// ------------------------------- CacheThread ------------------------------------

struct CACHE_REQUEST
{
    CACHE_REQUEST(){}
    CACHE_REQUEST(QString strFileName, QListWidgetItem *pListWidgetItem) :
            m_strFileName(strFileName), m_pListWidgetItem(pListWidgetItem){}

    QString m_strFileName;
    QListWidgetItem *m_pListWidgetItem;
};

struct CACHE_DATA
{
    CACHE_DATA(){}
    CACHE_DATA(QPixmap pixMap ,QListWidgetItem *pListWidgetItem) :
            m_pixMap(pixMap), m_pListWidgetItem(pListWidgetItem) {}

    QPixmap m_pixMap;
    QListWidgetItem *m_pListWidgetItem;
};

class CacheThread : public QThread
{
    Q_OBJECT
public:
    QPixmap GetPixMap(QString strFileName, QListWidgetItem *pListWidgetItem);
    void CacheFiles(QList<CACHE_REQUEST> listPixCacheJob);
    void RemoveFromCache(QString strFileName);

protected:
    virtual void run();

signals:
    void cacheAdded(QString strFileName, QListWidgetItem *pItem);
    void cacheRemoved(QString strFileName, QListWidgetItem *pItem);

private:
    QMutex m_mutexSynch;
    QMap<QString,CACHE_DATA> m_mapCache;
    QList<CACHE_REQUEST> m_listPixCacheJob;
};


// ------------------------------- FormAlbumMixer ------------------------------------
namespace Ui {
    class FormAlbumMixer;
}

class FormAlbumMixer : public QWidget {
    Q_OBJECT
public:
    FormAlbumMixer(CProfile *pProfile, QWidget *parent = 0);
    ~FormAlbumMixer();

    void actionDelete();
    bool actionSave();
    void actionLoad();
    void actionAbortProcessing();
    void doReshape();

    bool isModified() const;

    void Log(QString strLogData);

    CProfile *m_pProfile;

protected:
    void changeEvent(QEvent *e);

private:
    QList<CACHE_REQUEST> GetPix2Cache();
    QString ExtractOneRandomFilename();
    //
    Ui::FormAlbumMixer *m_ui;
    bool m_bIsModified;
    bool m_bAbortProcessing;
    int m_nFilesCacheSize;  // initial cache size, needed to update progress bar
    QStringList m_strlistFilesCache;
    CacheThread m_threadCache;
    QMutex m_mutexSynch;
    QSemaphore m_semaphoreSynch;


private slots:
    void on_pushButtonDstDir_clicked();
    void on_pushButtonSrcDir_clicked();
    void on_pushButtonPreview_clicked();
    void on_pushButtonProcess_clicked();
    void on_listWidgetFiles_doubleClicked(QModelIndex index);
    void on_listWidgetFiles_currentRowChanged(int currentRow);

    void cacheAdded(QString strFileName, QListWidgetItem *pItem);
    void cacheRemoved(QString strFileName, QListWidgetItem *pItem);
};

#endif // FORMALBUMMIXER_H
